<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <div class="search">
      <el-form ref="search" :inline="true" :model="search">
        <!-- 设备编号 -->
        <el-form-item label="设备编号:" size="medium">
          <el-input
            v-model="search.innerCode"
            clearable
            placeholder="请输入"
          />
        </el-form-item>
        <el-button
          type="primary"
          class="my-primary1"
          icon="el-icon-search"
          @click="getEquipList"
        >查询</el-button>
      </el-form>
    </div>

    <!-- 表单区域 -->
    <div class="result">
      <!-- 头部 -->
      <!-- 表单主体 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          :index="(index) => (search.pageIndex - 1) * 10 + (index + 1)"
          label="序号"
          width="80"
        />
        <el-table-column prop="innerCode" label="设备编号" />
        <el-table-column prop="type.name" label="设备型号" />
        <el-table-column prop="node.name" label="详细地址" />
        <el-table-column prop="vmStatus" label="运营状态" />
        <el-table-column prop="status" label="设备状态" width="300">
          <template v-slot="{ row }">
            <div class="vm-status">
              <div class="item">
                <div :class="row.status[10001] ? 'success' : 'fail'">离线</div>
              </div>
              <div class="item">
                <div :class="row.status[10002] ? 'success' : 'fail'">货道</div>
              </div>
              <div class="item">
                <div :class="row.status[10003] ? 'success' : 'fail'">
                  传动轴
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <el-button
              type="text"
              class="el-button--primary-text"
              @click.native="showDetailDialog(row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表单底部 -->
      <div class="pagination-container">
        <el-pagination
          layout="slot,prev,next"
          :current-page.sync="search.pageIndex"
          prev-text="上一页"
          next-text="下一页"
          background
          hide-on-single-page
          :total="totalCount"
          :page-size="search.pageSize"
          @current-change="getEquipList"
        >
          <template order="-1">
            <span
              class="el-pagination__total"
            >共{{ totalCount }}条记录 第 {{ search.pageIndex }}/{{
              totalPage
            }}
              页</span>
          </template>
        </el-pagination>
      </div>
    </div>
    <!-- 新增弹窗 -->
    <!-- 查看详情 -->
    <el-dialog title="设备详情" :visible.sync="detailDialog" width="630px">
      <div style="width: 579px; margin: 0px auto">
        <el-row
          type="flex"
          justify="space-around"
          align="middle"
          class="sku-stats"
        >
          <el-col :span="6">
            销售量：
            <span class="number">{{ orderCount }}</span>
            <span class="unit">&nbsp;个</span>
          </el-col>
          <el-col :span="6">
            销售额：
            <span class="number">{{ orderAmount }}</span>
            <span class="unit">&nbsp;万元</span>
          </el-col>
          <el-col :span="6">
            补货次数：
            <span class="number">{{ supplyCount }}</span>
            <span class="unit">&nbsp;次</span>
          </el-col>
          <el-col :span="6">
            维修次数：
            <span class="number">{{ repairCount }}</span>
            <span class="unit">&nbsp;次</span>
          </el-col>
        </el-row>
        <div class="sku-title">商品销量（月）</div>
        <el-row class="sku-list">
          <el-col
            v-for="(item, index) in skuCollectList"
            :key="index"
            :span="6"
          >
            <el-row class="sku-item">
              <el-col :span="14">
                <div :title="item.skuName" class="sku-name">
                  {{ item.skuName }}
                </div>
              </el-col>
              <el-col :span="10">：{{ item.count }}</el-col>
            </el-row>
          </el-col>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getEquipList,
  getRepairCount,
  getSupplyCount,
  getSkuCollect,
  getOrderCount,
  getOrderAmount
} from '@/api/vm'
import moment from 'moment'
import store from '@/store'
export default {
  name: 'EquipStateContainer',
  data() {
    return {
      search: {
        pageIndex: 1,
        pageSize: 10,
        status: null,
        innerCode: null
      },
      repairCount: 0,
      supplyCount: 0,
      orderAmount: 0,
      orderCount: 0,
      skuCollectList: [],
      createUserId: store.getters.userId,
      partnerId: null,
      addFormDialog: false,
      detailDialog: false,
      today: moment().format('YYYY-MM-DD'), // 今天日期
      firstDayMonth: moment().startOf('month').format('YYYY-MM-DD'), // 本月第一天日期
      tableData: [],
      totalPage: 0,
      totalCount: 0,
      currentName: '',
      currentId: null,
      rules: {
        policyName: [{ required: true, message: '请输入', trigger: 'change' }],
        discount: [{ required: true, message: '请输入', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getEquipList()
  },
  methods: {
    // 查看详情
    async showDetailDialog(row) {
      console.log(row)
      const askMessage = {
        innerCode: row.innerCode,
        start: this.firstDayMonth,
        end: this.today,
        vmType: row.vmType,
        nodeId: row.nodeId,
        createUserId: this.createUserId
      }
      // 维修次数
      this.repairCount = await getRepairCount({ ...askMessage })
      // 补货次数
      this.SupplyCount = await getSupplyCount({ ...askMessage })
      // 商品销量
      this.skuCollectList = await getSkuCollect({ ...askMessage })
      // 销售量
      this.orderCount = await getOrderCount({
        innerCode: row.innerCode,
        start: moment().startOf('months').format('YYYY-MM-DD HH:mm:ss'),
        end: moment().format('YYYY-MM-DD HH:mm:ss')
      })
      // 销售额
      this.orderAmount = (await getOrderAmount({
        innerCode: row.innerCode,
        start: moment().startOf('months').format('YYYY-MM-DD HH:mm:ss'),
        end: moment().format('YYYY-MM-DD HH:mm:ss')
      }) / 1000000).toFixed(2)
      this.detailDialog = true
    },
    // 查找
    async getEquipList() {
      const res = await getEquipList({ ...this.search })
      console.log(res)
      this.totalCount = +res.totalCount
      this.totalPage = +res.totalPage
      this.tableData = res.currentPageRecords.map((item) => {
        item.vmStatus =
          item.vmStatus === 0
            ? '未投放'
            : item.vmStatus === 1
              ? '运营'
              : item.vmStatus === 3
                ? '撤机'
                : '无'
        return item
      })
    },
    // 新增
    // 删除
    // 关闭弹窗(清空表单)
    closeFn() {
      this.currentId = null
      this.addForm = {
        policyName: '',
        discount: null
      }
      this.$refs.addForm.resetFields()
      this.getEquipList()
      this.addFormDialog = false
    }
    // 修改
    // 确认
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/common.scss";
.search .el-form-item {
  margin-bottom: 0;
}

::v-deep .el-form-item__label {
  font-weight: normal;
}

.taskDetail .el-form-item {
  margin-bottom: 0;
}

.vm-status {
  display: flex;
  .item {
    margin-right: 10px;
  }
  .success {
    height: 23px;
    padding: 0 12px;
    border-radius: 14px;
    text-align: center;
    color: #fff;
    line-height: 23px;
    background-color: #3fc997;
  }
  .fail {
    height: 23px;
    padding: 0 12px;
    border-radius: 14px;
    text-align: center;
    color: #fff;
    line-height: 23px;
    background-color: #ffc020;
  }
}
</style>
